<template>
  <div>
    <!-- 库存结算 -->
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="组织" prop="org_code">
          <!-- <el-input v-model="query.org_code" name="org_code" clearable /> -->
          <el-select v-model="query.org_code" name="org_code" placeholder="请选择组织" clearable>
            <el-option :label="item.label" :value="item.value" v-for="item in orgList" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="库位" prop="location">
          <el-input v-model="query.location" name="location" clearable />
        </el-form-item>
        <el-form-item label="品号" prop="material_no">
          <el-input v-model="query.material_no" name="material_no" clearable />
        </el-form-item>
        <el-form-item label="箱号" prop="lot_num">
          <el-input v-model="query.lot_num" name="lot_num" clearable />
        </el-form-item>
        <el-form-item label="品名" prop="material_name">
          <el-input v-model="query.material_name" name="material_name" clearable />
        </el-form-item>
        <el-form-item label="月份" prop="created_at">
          <el-date-picker v-model="query.created_at" type="month" placeholder="选择月份"  value-format="YYYY-MM" clearable />
        </el-form-item>
      </template>
    </Search>
    <div class="table-default" >
      <el-table :data="tableData" v-loading="loading" border show-summary ref="multipleTableRef">
        <el-table-column prop="org_name" label="组织" width="100" />
        <el-table-column prop="carogo_name" label="货主" width="100" />
        <el-table-column prop="material_no" label="品号" width="130" />
        <el-table-column prop="material.fname" label="品名" width="200" />
        <el-table-column prop="unit" label="单位" width="80" />
        <el-table-column prop="location" label="库位代码" width="100" />
        <el-table-column prop="locations.repositoryname" label="库位名称" width="100" />
        <el-table-column prop="wms_code" label="仓库代码" width="100" />
        <el-table-column prop="wms.fname" label="仓库名称" width="100" />
        <el-table-column prop="lot_num" label="箱号" width="180px" />
        <el-table-column prop="qty" label="库存数量" width="100">
          <template #default="scope">
            <span v-if="Number(scope.row.qty) == 0"></span>
            <span v-else>{{ Number(scope.row.qty) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="assign_qty" label="分配数量" width="110" sortable>
          <template #default="scope">
            <span v-if="Number(scope.row.assign_qty) == 0"></span>
            <span v-else>{{ Number(scope.row.assign_qty) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="release_qty" label="可用数量" width="100">
          <template #default="scope">
            <span v-if="Number(scope.row.release_qty) == 0"></span>
            <span v-else>{{ Number(scope.row.release_qty) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="月份" width="150px" />
        <el-table-column prop="supplier_code" label="来源单号" width="180px" />
        <!-- <el-table-column prop="created_at" label="创建时间" /> -->
      </el-table>
      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, watch } from 'vue'
import Create from './create.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { setTableHeight } from '/admin/utils/setHeight.js'
import { ElMessage } from 'element-plus'


const api = 'inventory/inventorysettlement'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)
console.log(tableData)


watch(query.value, (newValue, oldValue) => {
  // 处理逻辑
  console.log(newValue)
  console.log(newValue.location)
  if (newValue.location) {
    query.value.location_like = 1
  }
  console.log(query.value)
})

const orgList = ref([])

const getorg = () => {
  http.get(`options/Org`).then(r => {
    console.log('组织数据', r.data)
    orgList.value = r.data.data
  })
}

getorg()

onMounted(() => {
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>

<style lang="css">
.el-table {
  height: 87%;
}
</style>
